package com.farsight.leo.fssmartlock.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.farsight.leo.fssmartlock.R;
import com.farsight.leo.fssmartlock.net.bean.DeviceData;

import java.util.ArrayList;

import androidx.appcompat.widget.AppCompatTextView;

public class DeviceListAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<DeviceData> list;

    public DeviceListAdapter(Context context, ArrayList<DeviceData> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if(convertView==null){
            convertView=LayoutInflater.from(context).inflate(R.layout.item_device_list,null );
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder= (ViewHolder) convertView.getTag();
        }
        DeviceData data = list.get(position);
        holder.tvId.setText(String.valueOf(data.getDevice_id()));
        holder.tvName.setText(data.getDevice_name());
        return convertView;
    }

    private class ViewHolder {
        AppCompatTextView tvName;
        AppCompatTextView tvId;

        private ViewHolder(View convertView) {
            tvId=convertView.findViewById(R.id.item_device_list_tv_dev_id);
            tvName=convertView.findViewById(R.id.item_device_list_tv_dev_name);
        }
    }
}
